<!doctype html>
<html>

	<head>
		<meta charset="UTF-8">
		<title>注册</title>
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<link href="../css/mui.min.css" rel="stylesheet" />
		<link href="../style/fjol_style.css" rel="stylesheet" />
		<link rel="stylesheet" type="text/css" href="../style/xol_style.css"/>
	</head>

	<body>
		<header class="mui-bar mui-bar-nav newbgRed">
		    <a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left">
		    </a>
		    <h1 class="mui-title">账号注册</h1>
		</header>
		<div class="mui-content">
			<div class="mui-input-row mt10 m10">

				<input id="mobile" type="text" placeholder="输入手机号" style="width: 66%;" class="mui-input-clear">
				<span id="btnCode" class="mui-pull-right" style="background: #FFFFFF; text-align: center; padding: 8px 15px;">获取验证码</span>
			</div>
			<div class="mui-input-row mt5 m10">

				<input id="code" type="text" placeholder="输入验证码" class="mui-input-clear">

			</div>
			<div class="mui-input-row mt5 m10">

				<input id="password" type="text" placeholder="输入不少于6位的新密码" class="mui-input-clear">

			</div>

			<div class="m10">
				<button id="submit" type="button" class="mui-btn newbgRed colorWhite mui-btn-block">提交</button>
			</div>
		</div>
		<script src="../js/mui.min.js"></script>
		<script src="../js/common.util.js"></script>
		<script language='javascript' src='../js/md5.js'></script>
		<script src="../js/network.js"></script>
		<script type="text/javascript">
			mui.init();
			var _pageTip;
			mui.plusReady(function(){
				var wv = plus.webview.currentWebview();
				_pageTip = wv.pageTip;
//				console.log(_pageTip);
				if(_pageTip == 'forgetPassword'){		//忘记密码
					document.querySelector('.mui-title').innerHTML = '重置密码';
					document.querySelector('#submit').innerHTML = '重置密码';
				}else if(_pageTip == 'beUser'){		//忘记密码
					document.querySelector('.mui-title').innerHTML = '账号注册';
					document.querySelector('#submit').innerHTML = '提交';
				}
			})
			var net = fjNetwork;
			var isSend = true; //可以发送
			var isRegister = true; //可以注册
			function countdown(s) {
				var sendObj = document.querySelector('#btnCode');
				s--;
				if(s == 0) {
					sendObj.innerHTML = '获取验证码';
					isSend = true;
				} else {
					sendObj.innerHTML = s + 's后可重发';
					setTimeout(function() {
						countdown(s)
					}, 1000)
				}
			}
			//获取验证码
			document.querySelector('#btnCode').addEventListener('tap', function() {
				if(isSend) {
					var mobile = document.querySelector('#mobile').value;
					if(mobile.length == 0) {
						mui.toast('请输入手机号');
						return;
					}
					if(!common.validateMobile(mobile)) {
						mui.toast('请输入正确的手机号');
						return;
					}
					var param = [{
						key:'mobile',
						value:mobile
					}];
					net.get('user/mobileCode.json', param, function(data) {
						//成功
//						console.log(JSON.stringify(data));
						mui.toast(data.message);
						countdown(60);
						isSend = false;

					}, function(message) {
						//失败
						mui.toast(message);
					});
				}

			});
			document.querySelector('#submit').addEventListener('tap', function() {
				var mobile = document.querySelector('#mobile').value;
				var code = document.querySelector('#code').value;
				var password = document.querySelector('#password').value;
				if(mobile.length == 0) {
					mui.toast('请输入手机号');
					return;
				}
				if(!common.validateMobile(mobile)) {
					mui.toast('请输入正确的手机号');
					return;
				}
				
				if(password.length < 6) {
					mui.toast('请输入长度至少6位数的密码');
					return;
				}
				
				if(_pageTip == 'beUser'){
					if(isRegister) {
						
						var param = [{
							key: 'mobile',
							value: mobile
						}, {
							key: 'password',
							value: hex_md5(password)
						}, {
							key: 'code',
							value: code
						}, {
							key: 'userType',
							value: 1
						}];
						isRegister = false;
						net.post('user/register.json', param, function(data) {
							//成功
							isRegister = true;
							mui.toast(data.msg);
							
							setTimeout(function() {
								mui.back();
							}, 1000)
	
						}, function(message) {
							//失败
							isRegister = true;
							mui.toast(message);
						});
					}
				}else if(_pageTip == 'forgetPassword'){
					var paramCode = [{
						key: 'mobile',
							value: mobile
						}, {
							key: 'password',
							value: hex_md5(password)
						}, {
							key: 'code',
							value: code
						}];
						net.post('user/modify-pwd-by-code.json', paramCode, function(data) {
							//成功
//							isRegister = true;
							mui.toast(data.msg);
//							console.log(data.msg);
							setTimeout(function() {
								mui.back();
							}, 1000)
	
						}, function(message) {
							//失败
							isRegister = true;
							mui.toast(message);
						});
				}
				
			});
		</script>
	</body>

</html>